#pragma once
#include <iostream>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static const std::string default_path = "/";
static const std::string outputfile = "/dev/null";
void Daemon(bool chdircwd, bool iscloesfd)
{
    // 先忽略信号
    signal(SIGCHLD, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);

    pid_t pid = fork();

    (void)setsid();

    if (pid > 0)
        exit(0);

    if (chdircwd)
    {
        ::chdir(default_path.c_str());
    }

    if (iscloesfd)
    {
        ::close(0);
        ::close(1);
        ::close(3);
    }
    else
    {
        int fd = ::open(outputfile.c_str(),O_RDWR);
        if(fd > 0)
        {
            ::dup2(fd,0);
            ::dup2(fd,1);
            ::dup2(fd,2);
            ::close(fd);
        }
    }
}